#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=147744151,N=1005;
template <typename T> inline void read(T &x) {
    x = 0; char ch = getchar(); int f = 1;
    while (!isdigit(ch) && ch ^ '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); x *= f;
}
int n,m,q,P,x1,y1,x2,y2,nxt[N][N];
ll a[N][N],ans,b[N][N],sum;
inline ll ny(ll x){
	ll ans=1;int y=P-2;
	if(x==1) return 1;
	while(y){
		if(y&1) ans=ans*x%P;
		x=x*x%P;
		y>>=1;
	}return ans;
}
inline void get200(){
	ans=1;
	for(int i=x1;i<=x2;i++)
	for(int j=y1;j&&j<=y2;j=nxt[i][j])
	ans=ans*b[i][j]%P;
}
bool z=1;
inline void get_zhi(){
	ans=a[x2][y2]*ny(a[x2][y1-1])%P*ny(a[x1-1][y2])%P*a[x1-1][y1-1]%P;
}
int main(){
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
	read(n),read(m),read(P);
	for(long long i=2;i*i<=P;i++) 
		if(P%i==0){z=0;break;}
	a[0][0]=1;
	for(int i=1;i<=n;i++) a[i][0]=1;
	for(int i=1;i<=m;i++) a[0][i]=1;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++){
		read(b[i][j]);
		if(z) a[i][j]=b[i][j]*a[i-1][j]%P*a[i][j-1]%P*ny(a[i-1][j-1])%P;
	}
	for(int i=1;i<=n;i++)
	for(int j=m;j;j--)
		nxt[i][j]=(b[i][j+1]^1?(j+1):nxt[i][j+1]);
	read(q);
	for(int k=1;k<=q;k++){
		read(x1),read(y1),read(x2),read(y2);
		if(z&&!(n<=500&&m<=500)) get_zhi();
		else get200();
		sum=(sum+(k^ans))%mod;
	}
	printf("%lld",sum);
	return 0;
}

